package gqz.mvvm.view.custom;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;

/**
 * Created by Administrator on 2018/1/29.
 */

public class FadeOutTextView extends View {

    private final String TAG = "FadeOutTextView--->";

    private Paint paint;

    private int width = 500, height = 800, halfW, halfH;
    private int textSize = 30, textHeight;
    private int displayLineNum = 3;//显示行数

    private ArrayList<MyFadeOutText> textlist = new ArrayList<>();

    public FadeOutTextView(Context context) {
        super(context);
        initPaint();
        init();
    }

    public FadeOutTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();
        init();
    }

    public FadeOutTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
        init();
    }

    private void initPaint() {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(2);
        paint.setTextSize(textSize);
    }

    private void init() {
        float mDensity = getResources().getDisplayMetrics().density;
        textHeight = (int) ((mDensity + 1) * textSize) - 20;
        Log.d(TAG, "textHeight:" + textHeight);
        for (int i = 0; i < 3; i++) {
            addText("text " + i);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        height = textHeight * displayLineNum;
        Log.d(TAG, "height:" + height);

        width = getMeasureSize(widthMeasureSpec, width);
        height = getMeasureSize(heightMeasureSpec, height);
        Log.d(TAG, "width:" + width);
        Log.d(TAG, "height:" + height);

        halfW = width / 2;
        halfH = height / 2;

        setMeasuredDimension(width, height);
    }

    private int getMeasureSize(int measureSpec, int defaultSize) {
        int result = 0;
        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);

        if (mode == MeasureSpec.EXACTLY) {
            result = size;
        } else {
            result = defaultSize;
        }

        return result;
    }

    int count = 0;

    @Override
    protected void onDraw(final Canvas canvas) {
        super.onDraw(canvas);
        for (int i = 0; i < textlist.size(); i++) {
            drawText(canvas, textlist.get(i), 10, height - (textlist.size() - i) * textHeight);
        }
        fadeText();
        invalidate();
    }

    private void drawText(Canvas canvas, MyFadeOutText text, float x, float y) {
        paint.setColor(Color.argb(text.transparent, 0, 0, 0));
        canvas.drawText(text.text, x, y, paint);
    }

    private void fadeText() {
        for (int i = 0; i < textlist.size(); i++) {
            textlist.get(i).transparent--;
            if (textlist.get(i).transparent <= 0) textlist.remove(i);
        }
    }

    public void addText(String str) {
        if (textlist.size() < displayLineNum)
            textlist.add(new MyFadeOutText(str));
        else if (textlist.size() == displayLineNum) {
            textlist.remove(0);
            textlist.add(new MyFadeOutText(str));
        }
    }

    public void clearText(){
        textlist.clear();
    }

    public void setDisplayLineNum(int num) {
        displayLineNum = num;
    }

    class MyFadeOutText {
        String text = "";
        int transparent = 255;

        public MyFadeOutText(String text) {
            this.text = text;
        }
    }
}
